home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cpptutor.arc / OBJDYNAM.CPP < prev    next >
Text File  |  1991-04-28  |  1KB  |  67 lines

  1.                                  // Chapter 6 - Program 4
  2. #include "iostream.h"
  3.  
  4. class box {
  5.    int length;
  6.    int width;
  7. public:
  8.    box(void);             //Constructor
  9.    void set(int new_length, int new_width);
  10.    int get_area(void);
  11. };
  12.  
  13.  
  14. box::box(void)        //Constructor implementation
  15. {
  16.    length = 8;
  17.    width = 8;
  18. }
  19.  
  20.  
  21. // This method will set a box size to the two input parameters
  22. void box::set(int new_length, int new_width)
  23. {
  24.    length = new_length;
  25.    width = new_width;
  26. }
  27.  
  28.  
  29. // This method will calculate and return the area of a box instance
  30. int box::get_area(void)
  31. {
  32.    return (length * width);
  33. }
  34.  
  35.  
  36. main()
  37. {
  38. box small, medium, large;          //Three boxes to work with
  39. box *point;                        //A pointer to a box
  40.  
  41.    small.set(5, 7);
  42.    large.set(15, 20);
  43.  
  44.    point = new box;   // Use the defaults supplied by the constructor
  45.  
  46.    cout << "The small box area is " << small.get_area() << "\n";
  47.    cout << "The medium box area is " << medium.get_area() << "\n";
  48.    cout << "The large box area is " << large.get_area() << "\n";
  49.  
  50.    cout << "The new box area is " << point->get_area() << "\n";
  51.    point->set(12, 12);
  52.    cout << "The new box area is " << point->get_area() << "\n";
  53.  
  54.    delete point;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. // Result of execution
  61. //
  62. // The small box area is 35
  63. // The medium box area is 64
  64. // The large box area is 300
  65. // The new box area is 64
  66. // The new box area is 144
  67.